﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		//if (!Page.IsPostBack)
		//{
		//    for (int i = 0; i < 10; i++)
		//    {
		//        lbxCountry.Items.Add((i + 1).ToString());
		//    }
		//}

	}



	protected void btnAdd_Click(object sender, EventArgs e)
	{
		string strItemText = "";
		string strItemValue = "";

		strItemText = txtText.Text.Trim();
		strItemValue = txtValue.Text.Trim();

		ListItem li = new ListItem();
		li.Text = strItemText;
		li.Value = strItemValue;

		lbxCountry.Items.Add(li);

		lbxCountry.SelectedIndex = lbxCountry.Items.Count - 1;
		txtText.Text = "";
		txtValue.Text = "";

		ShowInfo();
		SetControlEnabled();

	}

	protected void ShowInfo()
	{
		string strOut = "";
		strOut = "目前下拉菜单中共有" + lbxCountry.Items.Count + "个选项<br/>";
		strOut += "当前选中的选项是第" + (lbxCountry.SelectedIndex + 1) + "项<br/>";
		strOut += "当前选中的项目的Text属性值为" + lbxCountry.SelectedItem.Text + "， Value属性值为"
			+ lbxCountry.SelectedValue + "<br>";

		lblInfo.Text = strOut;
	}
	protected void btnDisplay_Click(object sender, EventArgs e)
	{
		ShowInfo();
	}
	protected void lbxCountry_SelectedIndexChanged(object sender, EventArgs e)
	{
		ShowInfo();
	}

	protected void btnDelete_Click(object sender, EventArgs e)
	{
		//lbxCountry.Items.RemoveAt(lbxCountry.SelectedIndex);
		lbxCountry.Items.Remove(lbxCountry.SelectedItem);

		SetControlEnabled();
	}

	protected void SetControlEnabled()
	{
		if (lbxCountry.Items.Count == 0)
		{
			btnDelete.Enabled = false;
		}
		else
		{
			btnDelete.Enabled = true;
		}
	}
	protected void btnClear_Click(object sender, EventArgs e)
	{
		lbxCountry.Items.Clear();
		SetControlEnabled();
	}
}
